package com.jilian.mydemo.modul.user.activity;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.RxTimerUtil;
import com.jilian.mydemo.utils.UrlUtils;
import com.jilian.mydemo.utils.Utils;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.views.ClearEditText;
/**
 * 重置密码
 * weishixiong
 * 2019-03-17
 */
public class ResetPwdActivity extends BaseActivity {

    private ClearEditText etPhone;
    private ClearEditText etCode;
    private TextView tvGetCode;
    private ClearEditText etPwd;
    private RelativeLayout rlEye;
    private ImageView ivEye;
    private TextView tvOk;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }



    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    public void initView() {
        setNormalTitle("重置管理", v -> finish());
        etPhone = (ClearEditText) findViewById(R.id.et_phone);
        etCode = (ClearEditText) findViewById(R.id.et_code);
        tvGetCode = (TextView) findViewById(R.id.tv_get_code);
        etPwd = (ClearEditText) findViewById(R.id.et_pwd);
        rlEye = (RelativeLayout) findViewById(R.id.rl_eye);
        ivEye = (ImageView) findViewById(R.id.iv_eye);
        tvOk = (TextView) findViewById(R.id.tv_ok);
    }

    @Override
    public void initData() {

    }
    private int time = 60;
    private boolean mbDisplayFlg;
    @Override
    public void initListener() {


        rlEye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!mbDisplayFlg) {
                    // display password text, for example "123456"
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivEye.setImageResource(R.drawable.image_login_open);
                } else {
                    // hide password, display "."
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivEye.setImageResource(R.drawable.image_login_close);
                }
                etPwd.setSelection(etPwd.getText().toString().length());//将光标移至文字末尾
                mbDisplayFlg = !mbDisplayFlg;
                etPwd.postInvalidate();
            }
        });
        tvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Utils.checkPhoneNo(etPhone.getText().toString())) {
                    ToastUitl.showImageToastTips(getResources().getString(R.string.error_phone));
                    return;
                }
                getMsgCode();

            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(TextUtils.isEmpty(etPhone.getText().toString())){
                    tvGetCode.setEnabled(false);
                    tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title_dark));
                }
                else{
                    tvGetCode.setEnabled(true);
                    tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title));
                }
                updateEnableStatus();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateEnableStatus();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                updateEnableStatus();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Utils.checkPhoneNo(etPhone.getText().toString())) {
                    ToastUitl.showImageToastTips(getResources().getString(R.string.error_phone));
                    return;
                }
                if(!Utils.isLetterDigit(etPwd.getText().toString())){
                    ToastUitl.showImageToastTips(getResources().getString(R.string.error_pwd));
                    return;
                }
                resetPwd();
            }
        });
    }

    /**
     * 重置密码
     */
    private void resetPwd() {
        showLoadingDialog();
        Factoty.getViewModel(UserViewModel.class,ResetPwdActivity.this).updatePassword(etPhone.getText().toString(),etCode.getText().toString(),etPwd.getText().toString());
        Factoty.getViewModel(UserViewModel.class,ResetPwdActivity.this).getUpdateliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if(dto.isSuccess()){

                    ToastUitl.showImageToastSuccess("重置成功");
                    finish();
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }

    /**
     * 获取验证码
     */
    private void getMsgCode() {
        getLoadingDialog().showDialog();
        Factoty.getViewModel(UserViewModel.class,ResetPwdActivity.this).sendSms(etPhone.getText().toString(),3);
        Factoty.getViewModel(UserViewModel.class,ResetPwdActivity.this).getSmsliveData().observe(ResetPwdActivity.this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto  dto) {
                getLoadingDialog().dismiss();
                if (dto.getCode() == Constant.Server.SUCCESS_CODE) {
                    ToastUitl.showImageToastSuccess(dto.getMsg());
                    tvGetCode.setText("验证码("+String.valueOf(time + "s")+")");
                    tvGetCode.setEnabled(false);
                    tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title_dark));
                    RxTimerUtil.interval(1000, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext() {
                            if (time == 1) {
                                RxTimerUtil.cancel();
                                tvGetCode.setEnabled(true);
                                tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title));
                                tvGetCode.setText("获取验证码");
                                RxTimerUtil.cancel();
                                time = 60;
                                //设置手机号可编辑
                                etPhone.setEnabled(true);
                                //恢复图标
                                etPhone.setClearIconVisible(true);
                            } else {
                                time--;
                                tvGetCode.setText("验证码("+String.valueOf(time + "s")+")");
                                tvGetCode.setEnabled(false);
                                tvGetCode.setTextColor(getResources().getColor(R.color.color_main_title_dark));
                                //设置手机号不可编辑
                                etPhone.setEnabled(false);
                                //清除图标
                                etPhone.setClearIconVisible(false);
                            }

                        }
                    });
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }

    /**
     * 更新按钮的可用状态
     */
    private void updateEnableStatus() {
        if (!TextUtils.isEmpty(etPhone.getText().toString())
                && !TextUtils.isEmpty(etPwd.getText().toString())
                && !TextUtils.isEmpty(etCode.getText().toString())
                ) {
            tvOk.setEnabled(true);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_normal);
        } else {
            tvOk.setEnabled(false);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_dark);
        }
    }
}
